Skip to content

Conversation

siverne
Copy link
Member

@siverne siverne commented Nov 24, 2026

Closes #110

What

  • TOP_RATED sorts wines with at least 50 ratings first, then the rest. Within each part it sorts as before: average, then number of ratings, then id. The limit is TOP_RATED_MIN_COUNT in wine-query.ts.
  • A migration replaces the index wine_top_rated with one that follows the new order, so the first page is read from the index instead of sorting all 100 646 wines.
  • Tests for the new order, that the number of hits does not change, and that the other sort orders are unchanged.
  • README and docs/api.md explain the choice.

Why

The first page of «Best vurdert» was led by wines with 5.0 from 5–6 ratings, ahead of wines with 4.9 from over 150. On the Full dataset every wine has at least 5 ratings, and the median is about 58. With 50 as the limit, 56 333 wines (56 %) qualify.

Wines under the limit are not hidden, only sorted after the others, so totalCount is the same for every sort order. We considered a weighted average (as on IMDb), but it needs its own stored column to be fast.

How to test

  1. npm run db:migrate in backend/.
  2. npm run check from the root.
  3. The landing page and /sok with «Best vurdert»: no wine on the first page has fewer than 50 ratings.

After merge, on the VM

Run npm run db:migrate in backend/. Then check in psql that the index is used:

EXPLAIN SELECT * FROM wine
ORDER BY (community_count >= 50) DESC, community_avg DESC NULLS LAST, community_count DESC, id
LIMIT 25;

It should say Index Scan using wine_top_rated.

@siverne siverne requested a review from a team as a code owner November 24, 2026 13:23
Copy link
Member

@williasc williasc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small things to fix before merging:

README: the «Best vurdert» row replaced the «Rating average» row in the design-decisions table. It should be a new row, so we keep the reason for storing the average on the wine.
wine-query.test.ts: expect(query.first).toBe(24) was removed from the first test. Nothing in this PR is about page size, so please put it back.

Copy link
Member

@manerlan manerlan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, approving

@manerlan manerlan merged commit 0e9f4eb into main Nov 24, 2026
@manerlan manerlan deleted the 110-top-rated-min-count branch November 24, 2026 14:21
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

«Best vurdert» needs a minimum number of ratings
3 participants